#coding=utf-8
import logging
import os

import MySQLdb
from tornado import httpserver, web, ioloop
from apscheduler.scheduler import Scheduler
import thread
import threading

# 运行日志位置

__PORT__=8687
__LOG_FILE='./log/run.log'
logger = logging.getLogger()

def initLog():
    __DIR__ = "./log"
    if os.path.isdir(__DIR__) == False:
        os.makedirs(__DIR__)
    hdlr = logging.handlers.TimedRotatingFileHandler(__LOG_FILE, when='midnight', backupCount=30)
    formatter = logging.Formatter("[%(asctime)s]: %(module)s %(message)s ")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.DEBUG)
    logger.info("initialization log handler success! FILE=[%s]"%__LOG_FILE)

conn= MySQLdb.connect(
        host='127.0.0.1',
        port = 3306,
        user='root',
        passwd='',
        db ='test',
        )
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")


#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#删除查询条件的数据
#cur.execute("delete from student where age='9'")

# cur.execute("DELETE FROM kcd_diamond_cloud");

df = cur.execute("select * from user limit 10");

info = cur.fetchmany(df)

for i in info:
    print(i['create_time'])

cur.close()
conn.commit()
conn.close()

def test():
        print("test")

def testhread(s):
        print("testhread")
        print(s)


class versionHandler(web.RequestHandler):
    def get(self):
        self.write("hello world")



class Application(web.Application):
    def __init__(self):
        handlers = [
                (r"/version", versionHandler)
        ]
        settings = {
            "debug": True,
        }
        web.Application.__init__(self, handlers, **settings)


def http_server():
    server = httpserver.HTTPServer(Application)
    server.listen(__PORT__)
    ioloop.IOLoop.current().start()


def main():
    return
    # crontab
    sched = Scheduler()
    sched.add_interval_job(test, seconds=5)
    sched.start()

    thread.start_new_thread(testhread,('args'))
    initLog()
    http_server()


if __name__ == '__main__':
        main()